﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    // 形状接口
    public interface IShape
    {
        double CalculateArea();
        bool IsValid();
    }

    // 抽象形状基类
    public abstract class Shape : IShape
    {
        public abstract double CalculateArea();
        public abstract bool IsValid();
    }

    // 长方形类
    public class Rectangle : Shape
    {
        public double Length { get; set; }
        public double Width { get; set; }

        public Rectangle(double length, double width)
        {
            Length = length;
            Width = width;
        }

        public override double CalculateArea()
        {
            return Length * Width;
        }

        public override bool IsValid()
        {
            return Length > 0 && Width > 0;
        }

        public override string ToString()
        {
            return $"长方形[长={Length}, 宽={Width}]";
        }
    }

    // 正方形类
    public class Square : Rectangle
    {
        public Square(double side) : base(side, side) { }

        public override string ToString()
        {
            return $"正方形[边长={Length}]";
        }
    }

    // 三角形类
    public class Triangle : Shape
    {
        public double SideA { get; set; }
        public double SideB { get; set; }
        public double SideC { get; set; }

        public Triangle(double a, double b, double c)
        {
            SideA = a;
            SideB = b;
            SideC = c;
        }

        public override double CalculateArea()
        {
            // 使用海伦公式计算面积
            double p = (SideA + SideB + SideC) / 2;
            return Math.Sqrt(p * (p - SideA) * (p - SideB) * (p - SideC));
        }

        public override bool IsValid()
        {
            // 三角形两边之和大于第三边
            return SideA > 0 && SideB > 0 && SideC > 0 &&
                   SideA + SideB > SideC &&
                   SideA + SideC > SideB &&
                   SideB + SideC > SideA;
        }

        public override string ToString()
        {
            return $"三角形[边A={SideA}, 边B={SideB}, 边C={SideC}]";
        }
    }
}
